package com.yijumao.shop.controller;

import com.yijumao.shop.domain.Item;
import com.yijumao.shop.domain.ItemCat;
import com.yijumao.shop.service.ItemCatService;
import com.yijumao.shop.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author konley
 * @date 2020-11-02 17:27
 */
@Controller
public class IndexController {
    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemCatService itemCatService;

    //显示首页
    @RequestMapping("/")
    public String index(Model model){
        // 主页显示热门和优惠商品
        List<Item> rmItems = itemService.selectByIflag(1);
        List<Item> yhItems = itemService.selectByIflag(2);
        model.addAttribute("rmItems",rmItems);
        model.addAttribute("yhItems",yhItems);
        return "index";
    }

    //统一获取请求参数显示页面
    @RequestMapping("/{page}")
    public String page(@PathVariable String page){
        return page;
    }

    //响应ajax请求分类信息
    @RequestMapping("/itemCat/list")
    @ResponseBody
    public List<ItemCat> list(){
        return itemCatService.selectAllByStatus(1);
    }

    //用户退出
    @RequestMapping("/out")
    public String outLogin(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/";
    }
}
